package com.pg.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.EipTemp;
import com.pg.entity.dd.EipOperation;
import com.pg.entity.dd.EipWorkOrder;
import com.pg.entity.dd.OrderView;
import com.pg.mapper.EipOperationMapper;
import com.pg.mapper.EipTempMapper;
import com.pg.mapper.EipWorkOrderMapper;
import com.pg.service.EipOperationService;
import com.pg.utils.ColumnUtil;
import com.pg.utils.DateFormat;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 报工信息 服务实现类
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Service
@DS("proxyDs")
@Primary
public class EipOperationServiceImpl extends ServiceImpl<EipOperationMapper, EipOperation> implements EipOperationService {

    @Autowired
    private EipTempMapper eipTempMapper;

    @Autowired
    private EipOperationMapper eipOperationMapper;

    @Autowired
    private EipWorkOrderMapper eipWorkOrderMapper;

    @Override
    public void updateOperation(EipTemp eipTemp) {
        LambdaQueryWrapper<EipTemp> eipTempQueryWrapper = new LambdaQueryWrapper<>();
        // 采购订单号
        if (StringUtils.isNotEmpty(eipTemp.getLinkPUID())) {
            eipTempQueryWrapper.eq(EipTemp::getLinkPUID, eipTemp.getLinkPUID());
        }
        EipTemp eipTempByLinkPUID = eipTempMapper.selectOne(eipTempQueryWrapper);
        if (eipTempByLinkPUID == null) {
            eipTemp.setPuid(UUID.randomUUID().toString());
            eipTempMapper.insert(eipTemp);
        } else {
            eipTemp.setPuid(eipTempByLinkPUID.getPuid());
            eipTempMapper.updateByPuid(eipTemp);
        }
        EipOperation eipOperation = new EipOperation();
        eipOperation.setIoflag("mod");
        LambdaQueryWrapper<EipOperation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipOperation::getPuid, eipTemp.getLinkPUID());
        eipOperationMapper.update(eipOperation, queryWrapper);
    }

    @Override
    public void logicalDelete(String puid) {
        eipOperationMapper.logicalDelete(puid);
    }

    @Override
    public void logicalDeleteBatch(List<String> asList) {
        for (String puid : asList) {
            eipOperationMapper.logicalDelete(puid);
        }
    }

    @Override
    public EipOperation selectByPuid(String puid) {
        EipOperation eipOperation = eipOperationMapper.selectById(puid);
        eipOperation.setRemark1(selectWarnMessage(eipOperation));
        return eipOperation;
    }

    @Override
    public EipOperation selectByInsideNo(String insideNo) {
        LambdaQueryWrapper<EipOperation> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(insideNo)) {
            queryWrapper.eq(EipOperation::getInsideNo, insideNo);
        }
        return eipOperationMapper.selectOne(queryWrapper);
    }

    @Override
    public List<OrderView> selectListByMap(Map<String, String> map) {
        return eipOperationMapper.selectListByMap(map.get("modifyTime"), map.get("ioflag"));
    }

    @Override
    public IPage<EipOperation> selectForList(Page<EipOperation> page, EipOperation eipOperation, Date planStartTimeBegin, Date planStartTimeFinish) {
        LambdaQueryWrapper<EipOperation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipOperation::getIsDeleted, "0");
        //全库唯一码
        if (StringUtils.isNotEmpty(eipOperation.getPuid())) {
            queryWrapper.eq(EipOperation::getPuid, eipOperation.getPuid());
        }//工单号
        if (StringUtils.isNotEmpty(eipOperation.getWoNo())) {
            queryWrapper.like(EipOperation::getWoNo, eipOperation.getWoNo());
        }//产品型号
        if (StringUtils.isNotEmpty(eipOperation.getProductionModel())) {
            queryWrapper.like(EipOperation::getProductionModel, eipOperation.getProductionModel());
        }//种类编码
        if (StringUtils.isNotEmpty(eipOperation.getSubclassCode())) {
            queryWrapper.like(EipOperation::getSubclassCode, eipOperation.getSubclassCode());
        }//工序编码
        if (StringUtils.isNotEmpty(eipOperation.getProcessCode())) {
            queryWrapper.like(EipOperation::getProcessCode, eipOperation.getProcessCode());
        }//工序名称
        if (StringUtils.isNotEmpty(eipOperation.getProcessName())) {
            queryWrapper.like(EipOperation::getProcessName, eipOperation.getProcessName());
        }//计划日期（起）
        if (planStartTimeBegin != null) {
            queryWrapper.ge(EipOperation::getPlanStartTime, planStartTimeBegin);
        }//计划日期（止）
        if (planStartTimeFinish != null) {
            queryWrapper.le(EipOperation::getPlanStartTime, planStartTimeFinish);
        }//生产对象名称
        if (StringUtils.isNotEmpty(eipOperation.getManufacturedName())) {
            queryWrapper.like(EipOperation::getManufacturedName, eipOperation.getManufacturedName());
        }
        if (StringUtils.isNotEmpty(eipOperation.getPurchaserHqCode())) {
            queryWrapper.eq(EipOperation::getPurchaserHqCode, eipOperation.getPurchaserHqCode());
        }
        queryWrapper.last("ORDER BY \"planStartTime\" DESC nulls last, \"woNo\" ASC , \"processCode\" ASC");
        List<EipOperation> eipOperationListTemp = new ArrayList<>();
        IPage<EipOperation> eipOperationPage = eipOperationMapper.selectPage(page, queryWrapper);
        for (EipOperation eipOperationTemp : eipOperationPage.getRecords()) {
            eipOperationTemp.setRemark1(selectWarnMessage(eipOperationTemp));
            eipOperationListTemp.add(eipOperationTemp);
        }
        eipOperationPage.setRecords(eipOperationListTemp);
        return eipOperationPage;
    }

    private String selectWarnMessage(EipOperation eipOperation) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String woNo = eipOperation.getWoNo();
        map.put(ColumnUtil.getName(EipOperation::getWoNo), woNo);
        map.put(ColumnUtil.getName(EipOperation::getIsDeleted), "0");
        List<EipWorkOrder> eipWorkOrders = eipWorkOrderMapper.selectByMap(map);
        if (!CollectionUtils.isEmpty(eipWorkOrders)) {
            flag = false;
        }
        String warnMessage = flag ? "10001" : "";
        Date planEndTime = eipOperation.getPlanEndTime();
        Date actualFinishDate = eipOperation.getActualFinishDate();
        if (planEndTime != null && actualFinishDate != null) {
            long datePoor = DateFormat.getDatePoor(actualFinishDate, planEndTime);
            if (datePoor > 30) {
                warnMessage = "".equals(warnMessage) ? "10003" : warnMessage + ";10003";
            } else if (datePoor < 0) {
                warnMessage = "".equals(warnMessage) ? "10004" : warnMessage + ";10004";
            } else {
                warnMessage = "".equals(warnMessage) ? "10005" : warnMessage + ";10005";
            }
        }
        return warnMessage;
    }
}
